import { _decorator, Component, EditBox, Input, instantiate, Label, Node, Prefab, resources, SpriteAtlas, SpriteFrame, UITransform, Vec3 } from 'cc';
import { Card } from './Card';
import { utils } from './utils';
import { Config } from './Config';
import { msgToast } from './msgToast';
const { ccclass, property } = _decorator;

@ccclass('LevelEditor')
export class LevelEditor extends Component {
    @property({
        type: [SpriteFrame],
        displayName: '图片数组'

    })
    public sp: SpriteFrame[] = [];

    @property(Node)
    public c: Node = null!

    @property(Prefab)
    public cardPrefab: Prefab = null!

    @property(Label)
    public cSum: Label = null!

    @property(EditBox)
    public CARD_X: EditBox = null

    @property(EditBox)
    public CARD_Y: EditBox = null


    @property(EditBox)
    public DATA: EditBox = null

    //加载地图
    private map: any[] = []

    private cTransform: UITransform = null
    start() {
        let self = this;
        this.cTransform = this.c.getComponent(UITransform)
        this.c.on(Input.EventType.MOUSE_DOWN, self.cClick, self)
        self.sp = []

        //加载卡牌图片
        let url = `card${1}/card`;
        resources.load(url, SpriteAtlas, (err: any, atlas) => {
            if (err) {
                return console.error("SpriteAtlas load failed, err:" + err.message);
            }
            for (let i = 0; i < Config.MAX_CARD; i++) {
                self.sp.push(atlas.getSpriteFrame(`c${i + 1}`));
            }
        })
    }
    /**
     * 遍历box是否相交
     */
    checkClickable() {
        for (let i: number = 0; i < this.map.length; i++) {
            let isOverlap: boolean = false;
            for (let j: number = i + 1; j < this.map.length; j++) {
                if (!isOverlap) {
                    isOverlap = utils.isBoxOverlap(this.map[i].x, this.map[i].y, this.map[j].x, this.map[j].y, Config.CARD_WIDTH, Config.CARD_HIGHT);
                }
            }
            //box相交开启蒙版-不可点击
            this.map[i].clickable = !isOverlap
        }
    }

    cClick(e) {
        let self = this
        let x = Number(e.getUILocationX().toFixed(0))
        let y = Number(e.getUILocationY().toFixed(0))
        let newPoint = self.cTransform.convertToNodeSpaceAR(new Vec3(x, y))
        let card = instantiate(self.cardPrefab)
        let component = card.getComponent(Card)
        let v = Math.floor(Math.random() * 18)

        component.init(self.sp[v]);
        component.clickable = true
        self.c.addChild(card)
        card.setPosition(new Vec3(newPoint.x, newPoint.y, 0))

        self.map.push({ x: Number(newPoint.x.toFixed(0)), y: Number(newPoint.y.toFixed(0)), card, v })

        self.cSum.string = self.map.length.toString()
        self.CARD_X.string = (self.map[self.map.length - 1].x).toString()
        self.CARD_Y.string = (self.map[self.map.length - 1].y).toString()
        self.DATA.string = self.getJsonStr()

        //card.on(Input.EventType.KEY_DOWN, self.cardClick, self)

        self.checkClickable()
        self.map.forEach(item => {
            item.card.getComponent(Card).clickable = item.clickable
        })
    }

    btnEdit() {
        let self = this
        let len = self.map.length
        let x = Number(self.CARD_X.string)
        let y = Number(self.CARD_Y.string)

        self.map[len - 1].x = x
        self.map[len - 1].y = y
        self.map[len - 1].card.setPosition(new Vec3(x, y, 0))
        self.checkClickable()
        self.DATA.string = self.getJsonStr()
        self.checkClickable()
        self.map.forEach(item => {
            item.card.getComponent(Card).clickable = item.clickable
        })
    }

    getJsonStr() {
        let d: any[] = []
        this.map.forEach(item => {
            d.push({
                x: item.x,
                y: item.y
            })
        })
        return JSON.stringify(d);
    }

    btnClear() {
        this.map = []
        this.c.destroyAllChildren()
        this.c.removeAllChildren()
        this.CARD_X.string = ''
        this.CARD_Y.string = ''
        this.DATA.string = ''
    }


    //showToast
    @property({ type: Prefab })
    public toastPrefab: Prefab = null!;
    /**
     * showToast
     * @param conent 内容
     */
    public showToast(conent: string) {
        let t = instantiate(this.toastPrefab)
        let script = t.getComponent(msgToast);
        script.showToast(conent)
        this.node.addChild(t)
    }

}


